
#include<iostream>
#include<Windows.h>
#include<easyx.h>
#include<fstream>
#include "Enemy.h"
#include "Goods.h"
#include "Map.h"
#include "Npc.h"
#include "Role.h"
#include "Save.h"
#include "Skill.h"
#include "Store.h"
#include "Bag.h"
#include "Fight.h"
#include"Read.h"


vector<int> Boss_skill_Id;

using namespace std;


int welcome() {
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0xe);

	for (int i = 0; i < 8; i++)
		cout << endl;
	cout << '\t' << '\t' << '\t' << "      0  0      	       0        	   0            " << endl;
	cout << '\t' << '\t' << '\t' << " 0000000 0      	    0  0  0     	   0     0   0  " << endl;
	cout << '\t' << '\t' << '\t' << "      0  0      	    00 0   00   	  0 0 000000000 " << endl;
	cout << '\t' << '\t' << '\t' << "     0   0      	   0   0 0  00  	  0  0   0   0  " << endl;
	cout << '\t' << '\t' << '\t' << "    0    0      	  0    00    0  	 0 0     0   0  " << endl;
	cout << '\t' << '\t' << '\t' << "    0    0      	      00        	0   0 0  00  0  " << endl;
	cout << '\t' << '\t' << '\t' << "    0    0      	    00          	       0 0 0 0  " << endl;
	cout << '\t' << '\t' << '\t' << "    0 0000      	  00    0        	 00000 0 0 0 0  " << endl;
	cout << '\t' << '\t' << '\t' << "   000   0      	 0000000000000  	     0   0   0  " << endl;
	cout << '\t' << '\t' << '\t' << "000 0    0      	0  0    0       	    0   00  00  " << endl;
	cout << '\t' << '\t' << '\t' << "    0    0      	   00000000000   	 0  0 00 000 0  " << endl;
	cout << '\t' << '\t' << '\t' << "    0    0      	   0    0       	  00     0   0  " << endl;
	cout << '\t' << '\t' << '\t' << "    0    0    0 	   00000000000   	   0     0   0  " << endl;
	cout << '\t' << '\t' << '\t' << "    0    0    0 	   0    0       	    0    0   0  " << endl;
	cout << '\t' << '\t' << '\t' << "  0 0     00000 	   00000000000 		    0  0 0 0 0  " << endl;
	cout << '\t' << '\t' << '\t' << "   0            	   0            	        0   0   " << endl;


	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0xb);

	string gameMenu = "   1.新的游戏  2.读取存档  3.退出游戏";
	cout << '\t' << '\t' << '\t' << '\t';
	cout << '\t' << "   ********************" << endl;
	cout << '\t' << '\t' << '\t' << '\t' << '\t' << "       游戏制作信息" << endl;
	cout << '\t' << '\t' << '\t' << '\t' << '\t' << "   ********************" << endl;
	cout << '\t' << '\t' << '\t' << '\t' << "   中国海洋大学2023级程序设计实践基础12组" << endl;
	cout << '\t' << '\t' << '\t' << '\t' << '\t' << "郑鑫，郑淑辉，周鑫，方文韬" << endl;
	cout << '\t' << '\t' << '\t' << '\t';
	for (int i = 0; i < gameMenu.length(); i++) {
		Sleep(50);
		cout << gameMenu[i];
	}
	cout << endl << endl << '\t' << '\t' << '\t' << '\t' << "   ";
	return 0;

}
//显示游戏背景
int Background()
{

	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
	system("cls");
	cout << "故事背景" << endl << endl;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);

	string background = " 混沌开元初世，美丽的中原大地上出现了许多武林高手，同时也出现了许多远古猛兽，分别占据了终南山、段云峰、幽兰谷等地，神秘恶魔修格斯占据了升龙谷，相传这些地带都藏有珍贵宝藏，可是数百年前往的人都是有去无回，杳无音讯了。至此，江湖上便少有此类消息了。许多武林高手也都创建了自己的帮派，立足各地，互不干涉，而在众多帮派中，唯有孔雀山庄鹤立鸡群，因为孔雀山庄庄主秋天鸣拥有孔雀翎这一威力巨大的武器 。但孔雀山庄从不靠孔雀翎欺压其他帮派，对江湖众多事务始终处于中立态度，建立了良好的声誉。";
	for (int i = 0; i < background.length(); i++) {
		Sleep(10);
		cout << background[i];
	}
	cout << endl;
	system("pause");
	system("cls");
	cout << endl;
	background = " 突然某一天，由青龙掌管的青龙会收服了南方各大镖局，占据了南方的萧月城、拜月教、炎烨岛、锦绣原等地，并准备向北方各大镖局进攻，镇远镖局总局长邓定侯为了预防青龙会的进攻，写下联盟信派遣送信骑兵送往各大镖局，可是青龙会早已料到此时，安排了手下追杀送信骑兵。无奈之下，邓定侯在一个漆黑的夜晚独自前往了孔雀山庄，想要借用孔雀翎来增强镇远镖局的实力。";
	for (int i = 0; i < background.length(); i++) {
		Sleep(10);
		cout << background[i];
	}
	cout << endl;
	system("pause");
	system("cls");
	cout << endl;
	background = "  可是邓定侯不知道的是，孔雀翎早在数十年之前就已丢失，铛...铛铛铛...，邓定侯焦急地敲着孔雀山庄的大门";
	for (int i = 0; i < background.length(); i++) {
		Sleep(10);
		cout << background[i];
	}
	cout << endl;
	system("pause");

	system("cls");
	cout << endl;
	background = " 哐当，大门被打开了，迎面而来的是一个青涩帅气的面孔...";
	for (int i = 0; i < background.length(); i++) {
		Sleep(10);
		cout << background[i];
	}
	cout << endl;
	system("pause");
	system("cls");


	return 0;
}

void newGame(Role player, Map map) {

	Goods goods[27] = { 0, 1, 2, 3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26 };
	Store store;
	int chatNum = 0;

	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
begin://后续goto语句返回结点
	Sleep(1500);
	system("cls");
	cout << endl << "位于 :" << map.getName() << endl;
	map.showRoom();
	if (map.isThereFight()) {
		cout << "这里有敌人：" << '\t';
		showEnemyName(map.getPosition());
	}
	player.showRole();											//显示人物信息
	cout << endl << "1.交谈 2.战斗 3.移动 4.背包 5.直接退出 6.保存并退出" << endl;
	string choice;
	while (true) {
		cin >> choice;
		if (choice !="1"  && choice != "2" && choice != "3" && choice != "4" && choice != "5" && choice != "6") {
			cout << "选择错误，请重新选择。" << endl;
			Sleep(100);
			goto begin;
		}

		else break;
	}
	if (choice == "1") {
		if (map.isThereChat()) {
			//交谈
			Npc npc(map.getPosition());
			map.setNpc(npc);
			cout << endl << "可以对话的人物:" << endl;
			cout << map.getNpcName() << endl;
			player = map.chatToNpc(player);
		}
	}
	else if (choice == "2")
	{
		if (map.isThereFight()) {
			Enemy* enemy = nullptr;
			//1,2,3属性依次为	id, 名字，血量，内力，护甲，攻击伤害，掉落金币,秘籍id
			if (map.getPosition() == 1 && EnemyCount[1] == 0)enemy = new Enemy_npc(1, "高立", 100, 50, 10, 5, 100, 21);
			else if (map.getPosition() == 2 && EnemyCount[2] == 0)enemy = new Enemy_npc(2, "麻锋", 100, 50, 30, 20, 175, 22);
			else if (map.getPosition() == 3 && EnemyCount[3] == 0)enemy = new Enemy_npc(3, "西门玉", 100, 50, 50, 35, 325, 23);
			//4是大BOSS青龙，打完通关，无掉落物,无金币
			else if (map.getPosition() == 4 && EnemyCount[4] == 0) {
				vector<int> skills = { 24,25,26 };
				enemy = new Enemy_Boss(4, "青龙", 300, 100, 500, 0, skills);
			}
			//5,6,7属性依次为id, 名字，血量，护甲，攻击伤害，掉落金币
			else if (map.getPosition() == 5)enemy = new Enemy(5, "雷霆巨蜥", 100, 50, 5, 50);
			else if (map.getPosition() == 7)enemy = new Enemy(6, "玄冰麒麟", 150, 15, 15, 125);
			else if (map.getPosition() == 8)enemy = new Enemy(7, "炽焰凤凰", 225, 25, 20, 275);
			//大怪--别踩白块儿
			else if (map.getPosition() == 9) enemy = new Enemy(9, "修格斯", 100, 30, 25, 500);

			else
			{
				cout << "这里没有战斗可以发生" << endl;
				goto begin;
			}
			Fight fight(player, enemy);
			system("cls");
			cout << "正在进入战斗......." << endl;
			Sleep(1200);
			system("cls");
			player = fight.Fighting();
			delete enemy;
			if (!fight.IsPlayerLive()) {
				player.newBag();
				goto begin;
			}
		}
		else
		{
			cout << "这里没有战斗可以发生" << endl;
			goto begin;
		}
	}

	else if (choice == "3") {
		while (true)
		{
begin4:			map.showMap();
			cout << "使用w a s d来移动 按1进入地图" << endl;
			char order;
			cin >> order;
			if (order == '1') {
				if (player.getMapId() != 6) {
					system("cls");
					break;
				}
				else {
					system("cls");
				begin5:	cout << "1.购买物品		2.售出物品		3.退出" << endl;

					int choiceStore;
					cin >> choiceStore;
					if (choiceStore == 1) {
						store.showStore();
						player = store.Buywares(player);
					}
					else if (choiceStore == 2) {
						player.showBag();
						player = store.Sellwares(player);

					}
					else if (choiceStore == 3) {
						goto begin4;
					}
					else {
						cout << "请重新输入" << endl;
						Sleep(100);
						system("cls");
						goto begin5;
					}

				}

			}
			else {
				map.Move(order);
				chatNum = 0;

				player.setMapId(map.getPosition());
			}
		}
	}
	else if (choice == "4") {
		int i;
		int id;
		cout << "你的背包有这些物品：" << endl;

		player.showBag();
	begin2: cout << "请选择你要使用的物品" << endl;
		cout << "1.装备 2.药品 3.退出" << endl;
		cin >> i;
		if (i == 1) {
			player.showEquip();
		begin3:			cout << "1.更换装备 2.脱下装备 3.退出" << endl;
			int option;
			cin >> option;
			if (option == 1) {
				cin >> id;
				player.wearEquip(id);
				goto begin2;
			}
			else if (option == 2) {
				player.removeEquip(id);
				goto begin2;
			}
			else if (option == 3) {

			}
			else {
				cout << "请重新输入" << endl;
				goto begin3;
			}
		}

		else if (i == 2) {
			player.useDrug();
			goto begin2;
		}
		else if (i == 3) {
			goto begin;
		}
		else {
			cout << "请重新输入" << endl;
			goto begin2;
		}

	}


	else if (choice == "5") {
		cout << "成功退出！" << endl;
		exit(0);
	}

	else if (choice == "6") {
		
		Save::setToFile(player);
		exit(0);
	}
	goto begin;
}



void readFile() {
	ifstream fileRole("SaveRole.dat", ios_base::in | ios_base::binary);
	ifstream fileBag("SaveBag.dat", ios_base::in | ios_base::binary);
	ifstream fileSkill("SaveSkill.dat");
	if (!fileRole) {
		cout << "没有保存的游戏！" << endl;
		cout << "请重新选择:" << endl;

	}
	else {
		string name;
		int type, health_max, health, Mp, attack, defend, money, mapId, story;
		fileRole >> name >> type >> health_max >> health >> Mp >> attack >> defend >> money >> mapId >> story;

		Role player(type);
		player.setHealth_max(health_max);
		player.setHealth(health);
		player.setMp(Mp);
		player.setAttack(attack);
		player.setDefend(defend);
		player.setMoney(money);
		player.setMapId(mapId);
		player.setStory(story);

		Skill skill;
		fileSkill.read(reinterpret_cast<char*>(&skill), sizeof(Skill));
	
		player.setRole_Skill(skill);
		


		for (int key2 = 27; !fileBag.eof();) {

			int key;
			int value;
			fileBag >> key >> value;
			if (key2 == key)
				break;
			player.addSaveGoodsToBag(key, value);
			key2 = key;

		}
		
		fileRole.close();
		fileBag.close();
		fileSkill.close();

		Map newMap(player.getMapId());

		cout << "读入成功！" << endl;
		newGame(player, newMap);
	}
}

int main() {
menu:	welcome();
    string choice;
	while (true) {
		cin >> choice;;
		if (choice != "1" && choice != "2" && choice != "3") {
			cout << "选择错误，请重新选择。" << endl;
			Sleep(500);
			goto menu;
		}

		else break;
	}


	if (choice == "1") {
		//Background();
		cout << "秋少侠，欢迎揭开这段故事的序幕。" << endl;
		system("pause");

		Role player(1);
		system("cls");
		Map map;
		newGame(player, map);
	}
	if (choice == "2") {

		//readFile();
		ifstream fileRole("SaveRole.dat", ios_base::in | ios_base::binary);
		ifstream fileBag("SaveBag.dat", ios_base::in | ios_base::binary);
		ifstream fileSkill("SaveSkill.dat");
		if (!fileRole) {
			cout << "没有保存的游戏！" << endl;
			cout << "请重新选择:" << endl;
			system("pause");
			goto menu;

		}
		else {
			string name;
			int type, health_max, health, Mp, attack, defend, money, mapId, story;
			fileRole >> name >> type >> health_max >> health >> Mp >> attack >> defend >> money >> mapId >> story;

			Role player(type);
			player.setHealth_max(health_max);
			player.setHealth(health);
			player.setMp(Mp);
			player.setAttack(attack);
			player.setDefend(defend);
			player.setMoney(money);
			player.setMapId(mapId);
			player.setStory(story);

			Skill skill;
			
			fileSkill.read(reinterpret_cast<char*>(&skill), sizeof(Skill));
			//player.setRole_Skill(skill);
				



			for (int key2 = 27; !fileBag.eof();) {

				int key;
				int value;
				fileBag >> key >> value;
				if (key2 == key)
					break;
				player.addSaveGoodsToBag(key, value);
				key2 = key;

			}
			fileRole.close();
			fileBag.close();
			fileSkill.close();
			Map newMap(player.getMapId());

			cout << "读入成功！" << endl;
			newGame(player, newMap);
		}

	}
	if (choice == "3")
		exit(0);
	return 0;
}